<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Business;
use App\Models\ServiceProject;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class BusinessController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $serviceProject = ServiceProject::query()->get()->toArray();
        $serviceProject=array_column($serviceProject,null,'id');

        return Grid::make(new Business(), function (Grid $grid) use ($serviceProject){
            $grid->column('id')->width(50)->sortable();
            $grid->column('start_date')->width(180);
            $grid->column('service_address')->display(function ($service_address) {
                return "<div >地址： $service_address </div >
<div >详细地址： $this->service_address_desc </div >";
            });

            $grid->column('service_project','服务详情')->width(100)->expand(function () use ($serviceProject){
                // 返回显示的详情
                // 这里返回 content 字段内容，并用 Card 包裹起来

//                [{"image": "14/g9jw73all1.jpg", "explain": "12"}]
                $item="<div>";
                foreach ($this->service_project_ids as $id){
                    $item.="<label class='badge badge-success ml-1'>【{$serviceProject[$id]['title']}】 </label>";
                }
                $item.="</div>";

                $item.='<div class="row">';



                foreach ($this->service_project_needs as $need){
                    $item.=<<<EOF
'<div class="col-2">
<figure class="figure">
  <img src="{$need['image']}" class="figure-img img-fluid rounded" alt="...">
  <figcaption class="figure-caption">{$need['explain']}</figcaption>
</figure>
</div>'
EOF;

                }
                $item.='</div>';

                $html=<<<EOF
$item
EOF;


                return "<div style='padding:10px 10px 0'>$html</div>";
            });

//            $grid->column('pet_ids');
//            $grid->column('service_project_ids');
//            $grid->column('service_project_needs');
//            $grid->column('personnel_id');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Business(), function (Show $show) {
            $show->field('id');
            $show->field('start_date');
            $show->field('service_address');
            $show->field('service_address_desc');
            $show->field('handover');
            $show->field('handover_other');
            $show->field('pet_ids');
            $show->field('service_project_ids');
            $show->field('service_project_needs');
            $show->field('personnel_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Business(), function (Form $form) {
            $form->display('id');
            $form->text('start_date');
            $form->text('service_address');
            $form->text('service_address_desc');
            $form->text('handover');
            $form->text('handover_other');
            $form->text('pet_ids');
            $form->text('service_project_ids');
            $form->text('service_project_needs');
            $form->text('personnel_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
